package com.woniuxy.common.utils;

import cn.hutool.jwt.JWTUtil;
import com.alibaba.fastjson2.JSONObject;
import com.woniuxy.common.dto.UserDto;
import org.springframework.http.server.reactive.ServerHttpRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户工具类
 */
public class UserUtil {
    public  static  final String TOKEN="token";
    public  static  final String USER="user";

    //适用于servlet
    public static UserDto getUser(HttpServletRequest request){
        String token = request.getHeader(TOKEN);
        return getUser(token);

    }

    //适用于gateway
    public static UserDto getUser(ServerHttpRequest request){
        String token = request.getHeaders().getFirst(TOKEN);
        return getUser(token);
    }

    public static UserDto getUser(String token){
        String json = JWTUtil.parseToken(token).getPayload(USER).toString();
        UserDto userDto = JSONObject.parseObject(json, UserDto.class);
        return userDto;
    }


}
